<?php

namespace app\admin\controller\finance;

use app\common\controller\Backend;

/**
 * 订单管理
 *
 * @icon fa fa-circle-o
 */
class Settlement extends Backend
{
    
    /**
     * Settlement模型对象
     * @var \app\admin\model\finance\Settlement
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\finance\Settlement;
        $this->view->assign("orderJobTypeList", $this->model->getOrderJobTypeList());
        $this->view->assign("isAbnormalList", $this->model->getIsAbnormalList());
        $this->view->assign("isRenewalList", $this->model->getIsRenewalList());
        $this->view->assign("statusList", $this->model->getStatusList());
    }

    public function import()
    {
        parent::import();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                    ->with(['user','hotel','company'])
                    ->where($where)
                    ->where(['settlement.status'=>['in','4,5,6']])
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $row) {
                $row->getRelation('user')->visible(['username']);
				$row->getRelation('hotel')->visible(['hotel_name']);
				$row->getRelation('company')->visible(['company_name']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    // 导出
    public function export()
    {
        $where = [];
        $param = $this->request->param();
        $param['hotel_hotel_name']!=null && $where['hotel.hotel_name'] = ['like','%'.$param['hotel_hotel_name'].'%'];
        $param['user_username']!=null && $where['user.username'] = ['like','%'.$param['user_username'].'%'];
        $param['status']!=null && $where['settlement.status'] = $param['status'];

        if($param['order_job_date'] !=null){
            $param['order_job_date'] = explode(' - ',$param['order_job_date']);
            $where['settlement.order_job_date'] = ['between',[$param['order_job_date'][0],$param['order_job_date'][1]]];
        }

        $list = $this->model
            ->with(['user', 'hotel', 'company'])
            ->where($where)
            ->select();

        if($list){
            $line[0] = [
                __('Id'),
                __('Hotel.hotel_name'),
                __('Order_job_date'),
                __('Order_job_type'),
                __('Time_hour'),
                __('小时工'),
                __('小时工打卡记录'),
                __('Duration_hour'),
                __('小时工打卡地点'),
                __('酒店打卡记录'),
                __('Company_hour'),
                __('酒店打卡地点'),
                __('Fix_hour'),
                __('结算时长'),
                __('结算单价'),
                __('结算总价'),
                __('Status'),
            ];

            $total_time = 0;
            foreach ($list as $index => $row) {
                $row->getRelation('user')->visible(['username']);
                $row->getRelation('hotel')->visible(['hotel_name']);
                $row->getRelation('company')->visible(['company_name']);
                $line_data            = $row->toArray();
                $line[$index + 1][0]  = $line_data['id'];
                $line[$index + 1][1]  = $line_data['hotel']['hotel_name'];
                $line[$index + 1][2]  = $line_data['order_job_date'];
                $line[$index + 1][3]  = $line_data['order_job_type_text'];
                $line[$index + 1][4]  = $line_data['time_hour'];
                $line[$index + 1][5]  = $line_data['user']['username'];
                $line[$index + 1][6]  = $line_data['user_first_time_text'] . "\n" . $line_data['user_second_time_text'];
                $line[$index + 1][7]  = $line_data['duration_hour'];
                $line[$index + 1][8]  = $line_data['user_first_location'] . "\n" . $line_data['user_second_location'];
                $line[$index + 1][9]  = $line_data['company_first_time_text'] . "\n" . $line_data['company_second_time_text'];
                $line[$index + 1][10] = $line_data['company_hour'];
                $line[$index + 1][11] = $line_data['company_first_location'] . "\n" . $line_data['company_second_location'];
                $line[$index + 1][12] = $line_data['fix_hour'];
                $line[$index + 1][13] = $line_data['total_time'];
                $line[$index + 1][14] = $line_data['price'];
                $line[$index + 1][15] = $line_data['all_price'];
                $line[$index + 1][16] = $line_data['status_text'];
                $total_time           += $line_data['total_time'];
            }

//            $count                = count($list);
//            $line[$count + 4][1]  = '日期：';
//            $line[$count + 4][2]  = $list[0]['order_job_date'];
//            $line[$count + 5][1]  = '酒店名称：';
//            $line[$count + 5][2]  = $list[0]['hotel']['hotel_name'];
//            $line[$count + 6][1]  = '今日结算总人数：';
//            $line[$count + 6][2]  = $count.'人';
//            $line[$count + 7][1]  = '今日结算总工时：';
//            $line[$count + 7][2]  = $total_time.'小时';
//            $line[$count + 5][10] = '确认签字：';
//            $line[$count + 7][10] = '日期：';
            $name                 = $list[0]['hotel']['hotel_name'] . '-' . $list[0]['order_job_date'].'.xlsx';
            unset($list);
            \office\ExportXls::export_excel($line, $name);
        }else{
            $this->error('没有找到匹配的记录');
        }
    }
}
